package com.fruit.controller.app.farmwork;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.pojo.common.Page;
import com.fruit.service.farmwork.BaseOperationManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

@Controller
@RequestMapping(value = "/app/operation")
public class OperationListController {
	private  Logger logger = LoggerFactory.getLogger(OperationListController.class);
	@Autowired
	private BaseOperationManager baseOperationService;
	
	/**
	 * 查看维护记录
	 * @param record 维护记录
	 * @return Map<String, Object> 
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET ,RequestMethod.POST  })
	@ResponseBody
	public Map<String, Object> list(String userId, Page page){
		logger.info("【列表维护记录】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if(StringUtils.isEmpty(userId)){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为空!"));
				return result;
			}
			if(page==null){
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入分页参数不能为空!"));
				return result;
			}
			logger.info("【列表维护记录】---【params】--userId=" + userId + "page=" + page.getPage() + "showNumber=" + page.getShowNumber());
			
			Map<String, Object> pam = new HashMap<String, Object>();
			pam.put("startNum",  (page.getPage() - 1) * page.getShowNumber());
			pam.put("pageNum", page.getShowNumber());
			pam.put("operatorId", userId);//操作人id
			
			List<Map<String, Object>> records = baseOperationService.getRecords(pam);
			int num = baseOperationService.countRecords(userId);
			logger.info("【列表维护记录】---【操作成功】--"); 
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
			result.put("data", records);//放入数据
			result.put("dataNum", num);//放入记录数 
		} catch (Exception e) {
			logger.error("【列表维护记录】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		logger.info("【列表维护记录】---【结束】--");
		return result;
	}
	
}
